import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/home/welcome',
    component: () => import('@/views/Home'),
    children: [
      { path: '/home/welcome', props: true, component: () => import('@/components/Welcome') },
      { path: '/home/users', component: () => import('@/views/Users') },
      {
        path: '/home/goods', component: () => import('@/views/Goods'),
        children:
          [{
            path: '/home/goods/addgoods',
            component: () => import('@/components/Goods/AddGoods')
          }
          ,{
            path: '/home/goods',
            component: () => import('@/components/Goods/Mgoods')
          }
          ]
      },
      { path: '/home/roles', component: () => import('@/views/Roles') },
      { path: '/home/rights', component: () => import('@/views/Rights') },
      { path: '/home/orders', component: () => import('@/views/Orders') },
      { path: '/home/categories', component: () => import('@/views/Categories') },
      { path: '/home/params', component: () => import('@/views/Params') }
    ]

  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {  //挂载路由导航守卫
  if (to.path === '/login') return next()
  let token = sessionStorage.getItem("token")  //获取token
  if (token !== null) return next()
  Message({
    message: '请先登录',
    type: 'warning',
    duration: 2000
  });
  next('/login')

})


export default router
